package com.fxd.shopping.product.service.impl;

import com.fxd.core.util.Query;
import com.fxd.core.util.PageUtils;
import com.fxd.shopping.product.param.AttrGroupRelationDelAndAddParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.fxd.shopping.product.dao.AttrAttrgroupRelationDao;
import com.fxd.model.product.AttrAttrgroupRelationEntity;
import com.fxd.shopping.product.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveBatch(List<AttrGroupRelationDelAndAddParam> params) {
        params.forEach((param) -> {
            List<AttrAttrgroupRelationEntity> list = this.baseMapper.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", param.getAttrId()));
            if (CollectionUtils.isNotEmpty(list)) {
                AttrAttrgroupRelationEntity relationEntity = list.get(0);
                relationEntity.setAttrGroupId(param.getAttrGroupId());
                this.baseMapper.updateById(relationEntity);
            } else {
                AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
                BeanUtils.copyProperties(param, relationEntity);
                this.baseMapper.insert(relationEntity);
            }
        });
//        List<AttrAttrgroupRelationEntity> collect = params.stream().map((item) -> {
//            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
//            BeanUtils.copyProperties(item, relationEntity);
//            return relationEntity;
//        }).collect(Collectors.toList());
//        this.saveBatch(collect);
    }

}